<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{Set, Banner, Label};
use app\common\validate\{BannerValidate, LabelValidate};

class SetController extends BaseController
{
    /** 渲染系统参数页面 */
    public function set_edit()
    {
        View::assign('info', Set::find());
        return view();
    }

    /** 系统参数修改 */
    public function set_edit_data()
    {
        $data = input('post.');

        try{
            validate(SetValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $setModel = new Set();

        $flag = $setModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }






    /** 渲染轮播列表页面 */
    public function banner_list()
    {
        return view();
    }

    /** 渲染轮播添加页面 */
    public function banner_add()
    {
        return view();
    }

    /** 渲染轮播编辑页面 */
    public function banner_edit()
    {
        $data = input('');

        try{
            validate(BannerValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $bannerModel = new Banner();

        $info = $bannerModel->getFind(['id' => $data['id']]);

        View::assign('info', $info['data']);
        return view();
    }

    /** 轮播数据列表 */
    public function banner_data()
    {
        $data = input('get.');

        try{
            validate(BannerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $bannerModel = new Banner();

        /** 查询数据总条数 */
        $count = $bannerModel->getNum([])['data'];

        /** 查询列表 */
        $list = $bannerModel->getList([], 1, $data['page'], $data['limit'], '*', 'id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 轮播添加 */
    public function banner_add_data()
    {
        $data['img'] = request()->file('img');

        try{
            validate(BannerValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $bannerModel = new Banner();

        $data['img']   = fileUp($data['img'], 'banner');
        $flag = $bannerModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 轮播修改 */
    public function banner_edit_data()
    {
        $data = input('post.');
        $data['img'] = request()->file('img');

        try{
            validate(BannerValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $bannerModel = new Banner();

        $data['img'] = fileUp($data['img'], 'banner');

        $flag = $bannerModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }

    /** 轮播删除 */
    public function banner_del()
    {
        $data = input('post.');

        try{
            validate(BannerValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $bannerModel = new Banner();

        $flag = $bannerModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }




    /** 渲染标签分类列表页面 */
    public function label_list()
    {
        return view();
    }

    /** 渲染标签分类添加页面 */
    public function label_add()
    {
        return view();
    }

    /** 渲染标签分类编辑页面 */
    public function label_edit()
    {
        $data = input('');

        try{
            validate(LabelValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        $info = $labelModel->getFind(['id' => $data['id']]);

        View::assign('info', $info['data']);
        return view();
    }

    /** 标签分类数据列表 */
    public function label_data()
    {
        $data = input('get.');

        try{
            validate(LabelValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        /** 查询数据总条数 */
        $count = $labelModel->getNum([])['data'];

        /** 查询列表 */
        $list = $labelModel->getList([], 1, $data['page'], $data['limit'], '*', 'id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 标签分类添加 */
    public function label_add_data()
    {
        $data = input('');

        try{
            validate(LabelValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        $flag = $labelModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 标签分类修改 */
    public function label_edit_data()
    {
        $data = input('post.');

        try{
            validate(LabelValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        $flag = $labelModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }

    /** 标签分类删除 */
    public function label_del()
    {
        $data = input('post.');

        try{
            validate(LabelValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $labelModel = new Label();

        $flag = $labelModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
